package com.kc.web.controller.business;

import com.kc.business.domain.BizGoodsCategory;
import com.kc.business.service.IBizGoodsCategoryService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品分类管理
 */
@RestController
@RequestMapping("/business/goodsCategory")
@Slf4j
public class BizGoodsCategoryController extends BaseController {
    @Autowired
    private IBizGoodsCategoryService goodsCategoryService;


    /**
     * 商品分类列表
     * @param bizAppGoodsCategory
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizGoodsCategory bizAppGoodsCategory){
        startPage();
        List<BizGoodsCategory> appGoodsCategoryList = goodsCategoryService.getList(bizAppGoodsCategory);
        return getDataTable(goodsCategoryService.buildTypes(appGoodsCategoryList));
    }
    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCategory:add')")
    @Log(title = "商品分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizGoodsCategory bizGoodsCategory) {
        return toAjax(goodsCategoryService.add(bizGoodsCategory));
    }
    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCategory:edit')")
    @Log(title = "商品分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizGoodsCategory bizGoodsCategory) {
        //log.info("vue提交数据{}",bizGoodsCategory);
        return toAjax(goodsCategoryService.edit(bizGoodsCategory));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('business:goodsCategory:remove')")
    @Log(title = "商品分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Integer id) {
        BizGoodsCategory queryBusinessType = new BizGoodsCategory();
        queryBusinessType.setPid(id);
        queryBusinessType.setIsDeleted(0);
        List<BizGoodsCategory> list = goodsCategoryService.getList(queryBusinessType);
        if (list.size() > 0) {
            return AjaxResult.error("请先删除所有子分类");
        }
        BizGoodsCategory bizGoodsCategory = new BizGoodsCategory();
        bizGoodsCategory.setId(id);
        bizGoodsCategory.setIsDeleted(1);
        return toAjax(goodsCategoryService.delete(bizGoodsCategory));
    }

}
